<?php


namespace App\Http\Controllers\Sale;


use App\Models\Areas;
use App\Models\Category;
use App\Models\Goods;
use App\Models\GoodsCustomfield;
use App\Models\GoodsCustomRent;
use App\Models\GoodsHuabei;
use App\Models\GoodsCustom;
use App\Models\GoodsLog;
use App\Models\GoodsSetmeal;
use App\Models\GoodsSpec;
use App\Models\GoodsAttr;
use App\Models\MemberPromise;
use App\Services\AlipayService;
use App\Services\WechatService;
use App\Services\ZlbService;
use Illuminate\Http\Request;
use App\Services\CommonService;
use App\Services\GoodsService;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;

class GoodsController extends BaseController
// class GoodsController
{
    /**
     * 获取商品列表
     */
    public function getGoodsList(Request $request){
        $data = $request->input();
        $list = Goods::AdminGoodsList($data);
        if($list){
            foreach ($list['data'] as $k=>$v){
                unset($v['shopname']);
                $pact_type = '';
                if(is_array(json_decode($v['pact_type'],true))){
                    foreach(json_decode($v['pact_type'],true) as $val){
                        $pact_type .= '('.GoodsService::getGoodsPactName($val).')';
                    }
                }else{
                    $pact_type = '('.GoodsService::getGoodsPactName($v['pact_type']).')';
                }
                $list['data'][$k]['pact_type'] = $pact_type;
                $list['data'][$k]['status_name'] = GoodsService::getGoodsStatusName($v['status']);
                $list['data'][$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                //获取按天按月
                $rentType = GoodsService::GOODS_RENT_TYPE_DAY;
                $list['data'][$k]['rent_type'] = $rentType;
                if(!empty($v['activity_type'])){
                    $activity_list = explode(',',$v['activity_type']);
                    foreach ($activity_list as $k1=>$v1){
                        $list['data'][$k]['activity'][$k1]['activity_title'] = GoodsService::getGoodsActivity($v1);
                        $list['data'][$k]['activity'][$k1]['activity_img'] = GoodsService::getGoodsActivityImg($v1);
                    }
                }
                $quantity= GoodsSetmeal::where([['goods_id', $v['id']],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->select('id','quantity')->get();
                $quantity = $quantity ? $quantity->toArray() : array();
                if(!empty($quantity)){
                    $stock = array_column($quantity,'quantity');
                    $list['data'][$k]['stock'] = array_sum($stock);
                }else{
                    $list['data'][$k]['stock'] = 0;
                }
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$list);
    }

    /**
     * 查看商品详情
     */
    public function getGoodsInfo(Request $request){
        $goods_id = $request->input('goods_id');
        if($goods_id){
            $goods = Goods::GoodsInfo($goods_id);
            if($goods){
                $goods['new_grade'] = GoodsService::getGoodsNewGrade($goods['new_grade']);
                $goods['pact_type'] = $goods['pact_type'];
                $goods['province'] = Areas::GetAreasById($goods['province_id']);
                $goods['city'] = Areas::GetAreasById($goods['show_ity']);
                $goods['status_name'] = GoodsService::getGoodsStatusName($goods['status']);
                $goods['convey_type1'] = GoodsService::getGoodsSendTypeName($goods['convey_type1']);
                $goods['convey_type2'] = GoodsService::getGoodsReturnTypeName($goods['convey_type2']);
                unset($goods['province_id']);
                unset($goods['show_ity']);
                unset($goods['status']);
                $goods['attribute'] = GoodsCustom::getGoodsAttribute($goods['id']);

                //套餐信息
                $setmeal = GoodsSetmeal::getGoodsSetmeal($goods['id']);
                if($setmeal){
                    foreach ($setmeal as $k=>$v){
                            $spec = GoodsSpec::GetSpecList($v['id'],$v['goods_id']);
                            if(!empty($spec)){
                                foreach ($spec as $key=>$val){
                                    $spec[$key]['lease_time'] = $val['lease_time'].'天';
                                }
                            }

                        $setmeal[$k]['spec'] = $spec;
                        $setmeal[$k]['is_buy_out'] = GoodsService::getGoodsBuyoutName($v['is_buy_out']);
                        $setmeal[$k]['is_huabei'] = GoodsService::getGoodsIsHuabei($v['is_huabei']);
                    }
                }
                $goods['setmeal'] = $setmeal;
            }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$goods);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'缺少参数');
        }
    }

    /**
     * 获取一级品类
     */
    public function getCategory(Request $request){

        $category = Category::GetParentCate();

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$category);

    }

    /**
     * 获取二级品类
     */
    public function getSecondCategory(Request $request){

        $pid = $request->input('pid');

        $category = Category::GetSonCate($pid);

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$category);

    }



     /**
     * 商品分享
     */
    public function goodsShare(Request $request){
        $goods_id = $request->input('goods_id');
        //test
        $sale_user_id = $this->users['sale_user_id'];
        // $sale_user_id = 1;
        if($goods_id){
            $goods = Goods::where([['id',$goods_id],['status',GoodsService::GOODS_STATUS_PUT],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->select('id','title','imgurl','min_price')->first();
            $goods = $goods ? $goods->toArray() : array();
            if(!empty($goods)){
                $imageInfo = getimagesize(config('alipay.oss.imgHost').$goods['imgurl']);
                $goods['imgurl'] =  'data:' . $imageInfo['mime'] . ';base64,' . chunk_split(base64_encode(file_get_contents(config('alipay.oss.imgHost').$goods['imgurl'])));
                $goods['shara_url']= config('alipay.xxz.pcUrl').'/detail?id='.$goods_id.'&sale_user_id='.$sale_user_id;
                $url = urlencode('packageA/pages/productDetails/productDetails?__appxPageId='.mt_rand(100,999).'&goodsId='.$goods_id.'&saleuserid='.$sale_user_id);
                // $goods['alipay_url']= config('alipay.xxz.alipayUrl').$url;
                $res = AlipayService::createQrCode(10001, $goods_id,$goods['title'],$sale_user_id);
                if($res['status']){
                    $goods['qrcode_url'] = $res['data']['qr_code_url'];
                }
                $goods['alipay_url'] = 'https://h5.qianbaozuji.com?goodsId='.$goods_id.'&sale_user_id='.$sale_user_id;

                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$goods);
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '上架中的商品才能分享');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择商品');
        }
    }

    /**
     * 个人推广
     */
    public function userShare(Request $request){
        //test
        $sale_user_id = $this->users['sale_user_id'];
        // $sale_user_id = 1;
        $user['sale_user_id'] = $sale_user_id;

        $url = urlencode('pages/index/index?__appxPageId='.mt_rand(100,999).'&sale_user_id='.$sale_user_id);
        // $user['alipay_url']= config('alipay.xxz.alipayUrl').$url;
        $res = AlipayService::createUserQrCode($sale_user_id);
        $reswe = WechatService::createUserQrCode($sale_user_id);

        $reslink = WechatService::getLinkUrl($sale_user_id);
        $user['wx_url_link'] =$reslink['url_link'];

        $user['wx_qrcode_url'] = base64_encode($reswe['data']);
        $user['alipay_url'] = 'https://h5.qianbaozuji.com?sale_user_id='.$sale_user_id;
        if($res['status']){
            $user['qrcode_url'] = $res['data']['qr_code_url'];
        }else{
            $user['qrcode_url'] = $res;
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$user);
    }


}
